﻿using Datacollection.Services.Logging;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Datacollection
{
    public partial class Setup : Form
    {
        public Setup()
        {
            InitializeComponent();
            LoadCurrentSettings();
        }

        //取消
        // 加载当前配置到控件
        private void LoadCurrentSettings()
        {
            // 从应用程序配置中读取（需在Properties.Settings中预先定义这些配置项）
            moid_txt.Text = Properties.Settings.Default.mo_id;
            machineSN_txt.Text = Properties.Settings.Default.machineSN;
            txtSavePath.Text = Properties.Settings.Default.SavePath;
            partID_txt.Text = Properties.Settings.Default.partID;
            deptID_txt.Text = Properties.Settings.Default.deptID;
            lineID_txt.Text = Properties.Settings.Default.lineID;
            test_Station_txt.Text = Properties.Settings.Default.test_Station;
            txtUsedCount.Text = Properties.Settings.Default.UsedCount;
            nudRetryCount.Value = Properties.Settings.Default.SerialRetryCount;
            chkAutoSave.Checked = Properties.Settings.Default.AutoSaveData;
            testchannelID_txt.Text = Properties.Settings.Default.testchannelID;
            tosMasterId_txt.Text = Properties.Settings.Default.tosMasterId;
        }

        // 保存按钮点击事件
        private void save_btn_Click(object sender, EventArgs e)
        {
            var logger = LogServiceLocator.Logger;
            try
            {
                // 1. 验证输入
                if (string.IsNullOrWhiteSpace(txtSavePath.Text))
                {
                    MessageBox.Show("请设置数据保存路径", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtSavePath.Focus();
                    return;
                }

                // 验证保存路径是否有效
                if (!Directory.Exists(txtSavePath.Text))
                {
                    DialogResult result = MessageBox.Show(
                        $"路径 {txtSavePath.Text} 不存在，是否创建？",
                        "路径验证",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question
                    );
                    if (result == DialogResult.Yes)
                    {
                        Directory.CreateDirectory(txtSavePath.Text);
                    }
                    else
                    {
                        return;
                    }
                }

                // 2. 保存配置到应用程序设置
                Properties.Settings.Default.mo_id = moid_txt.Text.Trim();
                Properties.Settings.Default.partID = partID_txt.Text.Trim();
                Properties.Settings.Default.deptID = deptID_txt.Text.Trim();
                Properties.Settings.Default.lineID = lineID_txt.Text.Trim();
                Properties.Settings.Default.test_Station = test_Station_txt.Text.Trim();
                Properties.Settings.Default.machineSN = machineSN_txt.Text.Trim();
                Properties.Settings.Default.SavePath = txtSavePath.Text.Trim();
                Properties.Settings.Default.SerialRetryCount = (int)nudRetryCount.Value;
                Properties.Settings.Default.AutoSaveData = chkAutoSave.Checked;
                Properties.Settings.Default.UsedCount= txtUsedCount.Text.Trim();
                Properties.Settings.Default.testchannelID = testchannelID_txt.Text.Trim();
                Properties.Settings.Default.tosMasterId = tosMasterId_txt.Text.Trim();

                // 持久化到配置文件（user.config）
                Properties.Settings.Default.Save();

                // 3. 提示并关闭
                MessageBox.Show("配置保存成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                logger.Info($"常规配置保存成功");
                this.DialogResult = DialogResult.OK; // 标记为保存成功
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存配置失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 取消按钮点击事件
        private void cancel_btn_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        // 选择文件路径
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.folderBrowserDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                this.txtSavePath.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
